package util;

import java.util.ArrayList;
import java.util.List;

import pojo.NguoiDungPOJO;
import pojo.PairValue;
import services.Protocol;


import com.google.android.gcm.server.Constants;
import com.google.android.gcm.server.Message;
import com.google.android.gcm.server.Message.Builder;
import com.google.android.gcm.server.MulticastResult;
import com.google.android.gcm.server.Result;
import com.google.android.gcm.server.Sender;

public class ClientSender {
	public static boolean send(String regId,PairValue param,int timeToLive) throws Exception{
		ArrayList<PairValue> params=new ArrayList<PairValue>();
		params.add(param);
		return send(regId,params,timeToLive);
	}
	public static boolean send(String regID, ArrayList<PairValue> params,int timeToLive) throws Exception {
		boolean rs = false;
		Message.Builder builder = new Builder();		
		builder.timeToLive(timeToLive);
		builder.collapseKey("asdsd");
		for (int i = 0; i < params.size(); i++) {
			builder.addData(params.get(i).getName(), params.get(i).getValue());
		}
		Message message = builder.build();
		
		Result result = GCMSender.getInstance().send(message, regID, Protocol.RETRIES_GCM_SENDER);
		if (result.getMessageId() != null) {
			String canonicalRegId = result.getCanonicalRegistrationId();
			if (canonicalRegId != null) {
				// Cập nhật lại regId nhé
			}
			rs = true;
		} else {
			String error = result.getErrorCodeName();
			if (error.equals(Constants.ERROR_NOT_REGISTERED)) {
				// Xóa regId này nhé
			}
		}
		return rs;
	}
	public static void send(ArrayList<String> regID,PairValue param,int timeToLive) throws Exception{
		ArrayList<PairValue> params=new ArrayList<PairValue>();
		params.add(param);
		send(regID,params,timeToLive);		
	}
	public static void send( ArrayList<String> regID, ArrayList<PairValue> params,int timeToLive) throws Exception {
		boolean rs = false;
		Message.Builder builder = new Builder();
		builder.timeToLive(timeToLive);
		builder.collapseKey("asd");
		for (int i = 0; i < params.size(); i++) {
			builder.addData(params.get(i).getName(), params.get(i).getValue());
		}
		Message message = builder.build();		
		MulticastResult multiResult = GCMSender.getInstance().send(message, regID, Protocol.RETRIES_GCM_SENDER);
		List<Result> results = multiResult.getResults();
		for (int i = 0; i < results.size(); i++) {
			Result result = results.get(i);
			if (result.getMessageId() != null) {
				String canonicalRegId = result.getCanonicalRegistrationId();
				if (canonicalRegId != null) {
					// Cập nhật lại regId=canonicalRegId nhé
				}
				rs = true;
			} else {
				String error = result.getErrorCodeName();
				if (error.equals(Constants.ERROR_NOT_REGISTERED)) {
					// Xóa regId này nhé
				}
			}
		}

	}
	
	//------------------------
	public static boolean send(String regId,PairValue param) throws Exception{
		ArrayList<PairValue> params=new ArrayList<PairValue>();
		params.add(param);
		return send(regId,params);
	}
	public static boolean send(String regID, ArrayList<PairValue> params) throws Exception {
		boolean rs = false;
		Message.Builder builder = new Builder();	
		for (int i = 0; i < params.size(); i++) {
			builder.addData(params.get(i).getName(), params.get(i).getValue());
		}
		Message message = builder.build();
		
		Result result = GCMSender.getInstance().send(message, regID, Protocol.RETRIES_GCM_SENDER);
		if (result.getMessageId() != null) {
			String canonicalRegId = result.getCanonicalRegistrationId();
			if (canonicalRegId != null) {
				// Cập nhật lại regId nhé
			}
			rs = true;
		} else {
			String error = result.getErrorCodeName();
			if (error.equals(Constants.ERROR_NOT_REGISTERED)) {
				// Xóa regId này nhé
			}
		}
		return rs;
	}
	public static void send(ArrayList<String> regID,PairValue param) throws Exception{
		ArrayList<PairValue> params=new ArrayList<PairValue>();
		params.add(param);
		send(regID,params);
	}
	public static void send( ArrayList<String> regID, ArrayList<PairValue> params) throws Exception {
		
		Message.Builder builder = new Builder();
		for (int i = 0; i < params.size(); i++) {
			builder.addData(params.get(i).getName(), params.get(i).getValue());
		}
		Message message = builder.build();
		
		MulticastResult multiResult = GCMSender.getInstance().send(message, regID, Protocol.RETRIES_GCM_SENDER);
		List<Result> results = multiResult.getResults();
		for (int i = 0; i < results.size(); i++) {
			Result result = results.get(i);
			if (result.getMessageId() != null) {
				String canonicalRegId = result.getCanonicalRegistrationId();
				if (canonicalRegId != null) {
					// Cập nhật lại regId=canonicalRegId nhé
				}
				
			} else {
				String error = result.getErrorCodeName();
				if (error.equals(Constants.ERROR_NOT_REGISTERED)) {
					// Xóa regId này nhé
				}
			}
		}
	}
//	public static void send(ArrayList<NguoiDungPOJO> lstNguoiHoc, ArrayList<PairValue> params) throws Exception{
//		
//	}
}
